/**
 * Copyright &copy; 2012-2017 <a href="https://vivo.com">vivo</a> All rights reserved.
 */
package com.vivo.dingtalk.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.vivo.dingtalk.entity.OaKqStatics;
import com.vivo.employee.entity.ViEmployee;
import com.vivo.employee.service.ViEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.vivo.dingtalk.entity.DingUserInfo;
import com.vivo.dingtalk.dao.DingUserInfoDao;

/**
 * 钉钉人员信息Service
 * @author jyangye
 * @version 2018-10-17
 */
@Service
@Transactional(readOnly = true)
public class DingUserInfoService extends CrudService<DingUserInfoDao, DingUserInfo> {

    @Autowired
    private DingUserInfoDao dingUserInfoDao;

    @Autowired
    private SystemService systemService;

	public DingUserInfo get(String id) {
		return super.get(id);
	}

    public DingUserInfo getByDingId(String dingId) {
	    DingUserInfo query = new DingUserInfo();
	    query.setDingId(dingId);
	    List<DingUserInfo> userInfoList = findList(query);

	    return userInfoList.size() > 0 ? userInfoList.get(0) : null;
    }

	/**
	 * 获取所有在组织机构用户信息
	 * @param dingCompId
	 * @return
	 */
	public List<DingUserInfo> getListByDingCompId(String dingCompId) {
		return dao.getListByDingCompId(dingCompId);
	}

	public List<DingUserInfo> getKqListByCompName(String compName) {
		return dao.getKqListByCompName(compName);
	}

    public DingUserInfo getByUnionid(String unionid) {
	    DingUserInfo query = new DingUserInfo();
	    query.setUnionid(unionid);
	    List<DingUserInfo> userInfoList = findList(query);

	    return userInfoList.size() > 0 ? userInfoList.get(0) : null;
    }

	public DingUserInfo getByMobile(String mobile) {
	    List<DingUserInfo> userList = dingUserInfoDao.getListByMobile(mobile);
	    if (userList.size() == 0) {
	        return null;
        }

	    if (userList.size() == 1) {
	        return userList.get(0);
        }

	    for (DingUserInfo userInfo : userList) {
	        if (userInfo.getCompId().equals(userInfo.getDingCompId())) {
	            return userInfo;
            }

	        if (!userInfo.getDingCompId().equals("1")) {
	            return userInfo;
            }
        }
	    return userList.get(0);
    }
	
	public List<DingUserInfo> findList(DingUserInfo dingUserInfo) {
		return super.findList(dingUserInfo);
	}
	
	public Page<DingUserInfo> findPage(Page<DingUserInfo> page, DingUserInfo dingUserInfo) {
		return super.findPage(page, dingUserInfo);
	}

	public List<OaKqStatics> getKqStaticsList(boolean isZjl) {
		OaKqStatics query = new OaKqStatics();
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1, 0, 0, 0);
		query.setBeginDateStr(sdf.format(cal.getTime()));

		cal.add(Calendar.MONTH, 1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		query.setEndDateStr(sdf.format(cal.getTime()));
		if (isZjl) {
			return dao.getDdKqStaticsListZjl(query);
		} else {
			return dao.getDdKqStaticsList(query);
		}
	}

	@Transactional(readOnly = false)
    public void saveDingUser(JSONObject jsonObject, String compId) {
	    String mobile = jsonObject.getString("mobile");

	    // 获取用户信息
        User user = systemService.getUserByLoginName(mobile);
        if (user == null) {
            return;
        }

        DingUserInfo dui = getByMobile(mobile);
        if (dui == null) {
            dui = new DingUserInfo();
        }

        dui.setEmpId(user.getEmployeeId());
        dui.setUserId(user.getId());
        dui.setAvatar(jsonObject.getString("avatar"));
        dui.setDingId(jsonObject.getString("dingId"));
        dui.setName(jsonObject.getString("name"));
        dui.setDingUserId(jsonObject.getString("userid"));
        dui.setOpenid(jsonObject.getString("openId"));
        dui.setEmail(jsonObject.getString("email"));
        dui.setPosition(jsonObject.getString("position"));
        dui.setMobile(mobile);
        dui.setCompId(compId);

        save(dui);
    }
	
	@Transactional(readOnly = false)
	public void save(DingUserInfo dingUserInfo) {
		super.save(dingUserInfo);
	}
	
	@Transactional(readOnly = false)
	public void delete(DingUserInfo dingUserInfo) {
		super.delete(dingUserInfo);
	}
	
}